# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=graphicsmagick
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.45
pkgrel=1
pkgdesc="An image viewing/manipulation program (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://www.graphicsmagick.org/"
msys2_references=(
  "cpe: cpe:/a:graphicsmagick:graphicsmagick"
  "cpe: cpe:/a:imagemagick:graphicsmagick"
)
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-ghostscript"
             "${MINGW_PACKAGE_PREFIX}-libheif"
             "${MINGW_PACKAGE_PREFIX}-libjxl"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "txt2html")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         "${MINGW_PACKAGE_PREFIX}-libltdl"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
         #"${MINGW_PACKAGE_PREFIX}-perl"
optdepends=("${MINGW_PACKAGE_PREFIX}-ghostscript: for Ghostscript support"
            "${MINGW_PACKAGE_PREFIX}-libheif: for HEIF support"
            "${MINGW_PACKAGE_PREFIX}-libjxl: for JPEG XL support")
options=('libtool')
source=(https://sourceforge.net/projects/graphicsmagick/files/${_realname}/${pkgver}/GraphicsMagick-${pkgver}.tar.xz
        pathtools.c
        pathtools.h
        001-relocate.patch)
sha256sums=('dcea5167414f7c805557de2d7a47a9b3147bcbf617b91f5f0f4afe5e6543026b'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90'
            '97e09c603fdf6ee49d7dfcc5a63d8dbb01f66c8c4857fe4115421a1d17e88464')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

# =========================================== #

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd GraphicsMagick-${pkgver}
  cp -fHv "${srcdir}"/pathtools.[ch] magick/

  apply_patch_with_msg \
    001-relocate.patch

  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  export lt_cv_deplibs_check_method='pass_all'

  ../GraphicsMagick-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    --with-quantum-depth=16 \
    --with-modules \
    --with-wmf \
    --with-xml \
    --with-lcms2 \
    --with-heif \
    --with-jp2 \
    --with-jxl \
    --with-webp \
    --with-jbig \
    --without-fpx \
    --without-dps \
    --without-perl \
    --without-x

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make -j1 DESTDIR="${pkgdir}" install
  local PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  # fix path references in some files.
  find "${pkgdir}"${MINGW_PREFIX}/bin -name "*-config" -exec \
    sed "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
  find "${pkgdir}"${MINGW_PREFIX}/ -name "*.la" -exec \
    sed "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
}
